package com.resourcestore.action;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.util.ValueStack;
import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.CourseFileSrcCatePrivateDao;
import com.resourcestore.dao.CourseDao;
import com.resourcestore.model.CourseFileSrcCatePrivate;
import com.resourcestore.model.Course;
import com.resourcestore.model.CourseFileSrcCatePublic;
import com.resourcestore.model.MajorFileSrcCate;

public class CourseFileSrcCatePrivateAction extends BaseAction {
	
	private CourseFileSrcCatePrivate courseFileSrcCatePrivate;
	private List<CourseFileSrcCatePrivate> listCourseFileSrcCatePrivate;
	private CourseFileSrcCatePrivateDao courseFileSrcCatePrivateDao = new CourseFileSrcCatePrivateDao();
	private Pagination pagination = new Pagination();
	private List<Course> listCourse;
	private CourseDao courseDao = new CourseDao();
	
	/**
	 * 查询返回List
	 * @throws ParseException 
	 * 
	 */
	public String listCourseFileSrcCatePrivate(){
		
		courseFileSrcCatePrivate = new CourseFileSrcCatePrivate();
		courseFileSrcCatePrivate.setIsPreCourseCate(-1);
		
		listCourseFileSrcCatePrivate = courseFileSrcCatePrivateDao.selectCourseFileSrcCatePrivate(courseFileSrcCatePrivate);
		
		// 将bigint数字处理为时间格式
		for (int i = 0; i < listCourseFileSrcCatePrivate.size(); i++) {
			
//			if(listCourseFileSrcCatePrivate.get(i).getCourseName()!=null){
//				listCourseFileSrcCatePrivate.get(i).setCourseName(listCourseFileSrcCatePrivate.get(i).getCourseName()+"("+listCourseFileSrcCatePrivate.get(i).getRealName()+")");
//			}
			
			try {
				listCourseFileSrcCatePrivate.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourseFileSrcCatePrivate
								.get(i).getGmtCreated()));
				listCourseFileSrcCatePrivate.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourseFileSrcCatePrivate
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//设置预置分类字符串
			if (listCourseFileSrcCatePrivate.get(i).getIsPreCourseCate() == 0) {
				listCourseFileSrcCatePrivate.get(i).setIsPreCourseCateString("否");
			} else if (listCourseFileSrcCatePrivate.get(i).getIsPreCourseCate() == 1) {
				listCourseFileSrcCatePrivate.get(i).setIsPreCourseCateString("是");
			}
			if(listCourseFileSrcCatePrivate.get(i).getCourseFileCateParentId()==-1){
				listCourseFileSrcCatePrivate.get(i).setCourseFileCateParentName("根分类");
			}
		}
		
		return "default";
	}
	
	
	/**
	 * 根据条件搜索，查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String searchListCourseFileSrcCatePrivate(){
		
		//对内容进行urldecoder解码
		try {
			courseFileSrcCatePrivate.setCourseFileCateName(new String(courseFileSrcCatePrivate.getCourseFileCateName().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcCatePrivate.setCourseName(new String(courseFileSrcCatePrivate.getCourseName().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcCatePrivate.setAuthor(new String(courseFileSrcCatePrivate.getAuthor().getBytes("iso-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//CourseFileCateParentId为0等同于无值替换为-2
		if(courseFileSrcCatePrivate.getCourseFileCateParentId()==0){
			courseFileSrcCatePrivate.setCourseFileCateParentId(-2);
		}
		pagination.setTotal(courseFileSrcCatePrivateDao.queryCourseFileSrcCatePrivateNum(courseFileSrcCatePrivate));
		listCourseFileSrcCatePrivate = courseFileSrcCatePrivateDao.selectCourseFileSrcCatePrivatePage(courseFileSrcCatePrivate,pagination);
		
		// 将bigint数字处理为时间格式
		for (int i = 0; i < listCourseFileSrcCatePrivate.size(); i++) {
//			if(listCourseFileSrcCatePrivate.get(i).getCourseName()!=null){
//				listCourseFileSrcCatePrivate.get(i).setCourseName(listCourseFileSrcCatePrivate.get(i).getCourseName()+"("+listCourseFileSrcCatePrivate.get(i).getRealName()+")");
//			}
			try {
				listCourseFileSrcCatePrivate.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourseFileSrcCatePrivate
								.get(i).getGmtCreated()));
				listCourseFileSrcCatePrivate.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourseFileSrcCatePrivate
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 设置预置分类字符串
			if (listCourseFileSrcCatePrivate.get(i).getIsPreCourseCate() == 0) {
				listCourseFileSrcCatePrivate.get(i).setIsPreCourseCateString("否");
			} else if (listCourseFileSrcCatePrivate.get(i).getIsPreCourseCate() == 1) {
				listCourseFileSrcCatePrivate.get(i).setIsPreCourseCateString("是");
			}
			if (listCourseFileSrcCatePrivate.get(i).getCourseFileCateParentId() == -1) {
				listCourseFileSrcCatePrivate.get(i).setCourseFileCateParentName("根分类");
			}
		}
		
		return newpath("/CourseFileSrcCatePrivate/searchCourseFileSrcCatePrivate.jsp");
	}
	
	/**
	 * 多级分类标题文字缩进	
	 */
	public void formatCourseFileSrcCatePrivate(List<CourseFileSrcCatePrivate> listMFSC){
		
		for (CourseFileSrcCatePrivate eachMFSC : listMFSC) {			
			for(int i=0;i<eachMFSC.getDepth();i++){
				eachMFSC.setCourseFileCateName("&nbsp;&nbsp;"+ eachMFSC.getCourseFileCateName());
			}
		}
	}
	
	/**
	 * 多级分类递归函数
	 * 
	 * @param parentChapter
	 * @return
	 */
	public List<CourseFileSrcCatePrivate> recursiveListCourseFileSrcCatePrivateTree(CourseFileSrcCatePrivate parentMFSC) {

		List<CourseFileSrcCatePrivate> treeMFSC= new ArrayList<CourseFileSrcCatePrivate>();
		List<CourseFileSrcCatePrivate> firstStepListMFSC = new ArrayList<CourseFileSrcCatePrivate>();// 一级目录
		firstStepListMFSC = courseFileSrcCatePrivateDao.selectCourseFileSrcCatePrivateByParentId(parentMFSC);
		if (null != firstStepListMFSC && firstStepListMFSC.size() > 0) {
			for (int i = 0; i < firstStepListMFSC.size(); i++) {
				treeMFSC.add(firstStepListMFSC.get(i));

				// 递归时，将majorFileCateParentId换为已查询出来的majorFileCateId
				int newParentChapterId = firstStepListMFSC.get(i).getCourseFileCateId();
				parentMFSC.setCourseFileCateParentId(newParentChapterId);

				treeMFSC.addAll(this.recursiveListCourseFileSrcCatePrivateTree(parentMFSC));
			}
		}
		//formatCourseFileSrcCatePrivate(treeMFSC);
		return treeMFSC;
	}
	
	/**
	 * 递归更新子分类depth,isPreCourseCate,courseId
	 * 
	 * @param parentChapter
	 * @return
	 */
	public void recursiveUpdateSubCourseFileSrcCatePrivateDepth(CourseFileSrcCatePrivate parentMFSC) {

		
		List<CourseFileSrcCatePrivate> firstStepListMFSC = new ArrayList<CourseFileSrcCatePrivate>();// 一级目录
		firstStepListMFSC = courseFileSrcCatePrivateDao.selectCourseFileSrcCatePrivateByParentId(parentMFSC);
		if (null != firstStepListMFSC && firstStepListMFSC.size() > 0) {
			for (int i = 0; i < firstStepListMFSC.size(); i++) {
		
				firstStepListMFSC.get(i).setDepth(parentMFSC.getDepth()+1);
				firstStepListMFSC.get(i).setIsPreCourseCate(parentMFSC.getIsPreCourseCate());
				firstStepListMFSC.get(i).setCourseId(parentMFSC.getCourseId());
				
				try {
					firstStepListMFSC.get(i).setGmtModified(CommonUtil.getSysTimeFormatNum());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				courseFileSrcCatePrivateDao.updateCourseFileSrcCatePrivate(firstStepListMFSC.get(i));
				
				// 递归时，将majorFileCateParentId换为已查询出来的majorFileCateId
				int newParentChapterId = firstStepListMFSC.get(i).getCourseFileCateId();
				parentMFSC.setCourseFileCateParentId(newParentChapterId);

				this.recursiveUpdateSubCourseFileSrcCatePrivateDepth(parentMFSC);
			}
		}


	}
	
	/**
	 * 预插入数据
	  */
	
	public String preInsertCourseFileSrcCatePrivate(){
			
		//listCourse = courseDao.selectCourse();
		
		return newpath("/CourseFileSrcCatePrivate/insertCourseFileSrcCatePrivate.jsp");		
	}
	
	
	/**
	 * 插入一条数据
	  */		
	public String insertCourseFileSrcCatePrivate(){
		
		try {
			courseFileSrcCatePrivate.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseFileSrcCatePrivate.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		courseFileSrcCatePrivateDao.insertCourseFileSrcCatePrivate(courseFileSrcCatePrivate);
		return redirect(this.getUrl());		
	}
	
	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateCourseFileSrcCatePrivate(){
		
		courseFileSrcCatePrivate = courseFileSrcCatePrivateDao.selectCourseFileSrcCatePrivateById(courseFileSrcCatePrivate);
		
		try {
			courseFileSrcCatePrivate.setGmtCreatedString(CommonUtil.getSysTimeFormatString(courseFileSrcCatePrivate.getGmtCreated()));
			courseFileSrcCatePrivate.setGmtModifiedString(CommonUtil.getSysTimeFormatString(courseFileSrcCatePrivate.getGmtModified()));
		} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		
		return newpath("/CourseFileSrcCatePrivate/updateCourseFileSrcCatePrivate.jsp");
	}
	/**
	 * 更新信息
	 * 
	 */
	public String updateCourseFileSrcCatePrivate(){
		
		try {
			//courseFileSrcCatePrivate.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseFileSrcCatePrivate.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		courseFileSrcCatePrivateDao.updateCourseFileSrcCatePrivate(courseFileSrcCatePrivate);
		
		courseFileSrcCatePrivate.setCourseFileCateParentId(courseFileSrcCatePrivate.getCourseFileCateId());
		this.recursiveUpdateSubCourseFileSrcCatePrivateDepth(courseFileSrcCatePrivate);
		
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteCourseFileSrcCatePrivate(){

		courseFileSrcCatePrivateDao.deleteCourseFileSrcCatePrivate(courseFileSrcCatePrivate);
		courseFileSrcCatePrivate.setCourseFileCateParentId(courseFileSrcCatePrivate.getCourseFileCateId());
		List<CourseFileSrcCatePrivate> listMfsc = new ArrayList<CourseFileSrcCatePrivate>();
		listMfsc = this.recursiveListCourseFileSrcCatePrivateTree(courseFileSrcCatePrivate);
		for(int i=0;i<listMfsc.size();i++){
			courseFileSrcCatePrivateDao.deleteCourseFileSrcCatePrivate(listMfsc.get(i));
		}
		return redirect(this.getUrl());
	}
	
	/**
	 * 批量删除信息
	 */
	public String deleteMoreCourseFileSrcCatePrivate(){
	
		courseFileSrcCatePrivateDao.deleteMoreCourseFileSrcCatePrivate(courseFileSrcCatePrivate);
		return redirect(this.getUrl());
	}

	
	public CourseFileSrcCatePrivate getCourseFileSrcCatePrivate() {
		return courseFileSrcCatePrivate;
	}

	public void setCourseFileSrcCatePrivate(CourseFileSrcCatePrivate courseFileSrcCatePrivate) {
		this.courseFileSrcCatePrivate = courseFileSrcCatePrivate;
	}

	public List<CourseFileSrcCatePrivate> getListCourseFileSrcCatePrivate() {
		return listCourseFileSrcCatePrivate;
	}

	public void setListCourseFileSrcCatePrivate(List<CourseFileSrcCatePrivate> listCourseFileSrcCatePrivate) {
		this.listCourseFileSrcCatePrivate = listCourseFileSrcCatePrivate;
	}


	public Pagination getPagination() {
		return pagination;
	}


	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}


	public List<Course> getListCourse() {
		return listCourse;
	}


	public void setListCourse(List<Course> listCourse) {
		this.listCourse = listCourse;
	}
	

}
